
package com.csc440.team2.activity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.csc440.team2.SmilConstants;
import com.csc440.team2.smil.SMILAudioComponent;
import com.csc440.team2.smil.SMILImageComponent;
import com.csc440.team2.smil.SMILMessage;
import com.csc440.team2.smil.SMILTextComponent;


public class AddComponents extends ListActivity
{
    private SMILMessage message;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null)
        {
            message = (SMILMessage) bundle.getSerializable(SmilConstants.MESSAGE);

            Button doneButton = new Button(this);
            doneButton.setText("Done");
            doneButton.setOnClickListener(new OnClickListener()
            {

                public void onClick(View v)
                {
                    Intent in = new Intent();
                    in.putExtra(SmilConstants.MESSAGE, message);
                    setResult(1, in);
                    finish();
                }

            });

            ListView lv = getListView();
            lv.addFooterView(doneButton);
            setListAdapter(new ArrayAdapter<String>(this, R.layout.second_menu, MENUOPTIONS));
            lv.setTextFilterEnabled(true);
            lv.setOnItemClickListener(new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View clickedView, int position, long clickedItemId)
                {
                    // have to set the new view
                    if (position == 0)
                    {
                        Intent in = new Intent(AddComponents.this, MessagePosition.class);
                        AddComponents.this.startActivityForResult(in, 1);
                    }
                    if (position == 1)
                    {
                	Intent in = new Intent(AddComponents.this, AddAudio.class);
                    	AddComponents.this.startActivityForResult(in, 2);
                    }
                    if (position == 2)
                    {
                	Intent in = new Intent(AddComponents.this, AddImage.class);
                    	AddComponents.this.startActivityForResult(in, 3);
                    }
                }
            });
        }
    }


    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onSaveInstanceState(Bundle bundle)
    {
        bundle.putSerializable(SmilConstants.MESSAGE, message);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
	if(resultCode == 1)
	{
	    SMILTextComponent comp = (SMILTextComponent) data.getExtras().get("test");
	    message.addComponent(comp);
	    System.out.println("activity returned");
	}
	if(resultCode == 2)
	{
	    SMILAudioComponent comp = (SMILAudioComponent) data.getExtras().get("test");
	    message.addComponent(comp);
	    System.out.println("activity returned");
	}
	if(resultCode == 3)
	{
	    SMILImageComponent comp = (SMILImageComponent) data.getExtras().get("test");
	    message.addComponent(comp);
	    System.out.println("activity returned");
	}
    }

    final static String[] MENUOPTIONS = new String[] {
            "TEXT COMPONENT", "AUDIO COMPONENT", "IMAGE COMPONENT", "VIDEO COMPONENT"
    };
}
